package com.ea.report.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    private static final int RETRY_DELAY = 10000;

    //  this will force queue declaration during app startup
    @Bean
    ApplicationRunner runner(ConnectionFactory cf) {
        return args -> cf.createConnection().close();
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // vibra
    @Bean
    public Queue vibraQueue() {
        return QueueBuilder.durable("video-analyze-vibra")
                .deadLetterExchange("ex_video-analyze-vibra_dlx")
                .build();
    }

    @Bean
    public Queue vibraRetryQueue() {
        return QueueBuilder.durable("video-analyze-vibra-retry")
                .deadLetterExchange("ex_video-analyze-vibra")
                .ttl(RETRY_DELAY)
                .build();
    }

    @Bean
    public FanoutExchange vibraExchange() {
        return ExchangeBuilder.fanoutExchange("ex_video-analyze-vibra").build();
    }

    @Bean
    public FanoutExchange vibraDlxExchange() {
        return ExchangeBuilder.fanoutExchange("ex_video-analyze-vibra_dlx").build();
    }

    @Bean
    public Binding bindingVibra(@Qualifier("vibraQueue")Queue queue, @Qualifier("vibraExchange")FanoutExchange ex) {
        return BindingBuilder.bind(queue).to(ex);
    }

    @Bean
    public Binding bindingVibraRetry(@Qualifier("vibraRetryQueue")Queue queue, @Qualifier("vibraDlxExchange")FanoutExchange ex) {
        return BindingBuilder.bind(queue).to(ex);
    }

    // shenshi

    @Bean
    public Queue shenshiQueue() {
        return QueueBuilder.durable("video-analyze-shenshi")
                .deadLetterExchange("ex_video-analyze-shenshi_dlx")
                .build();
    }

    @Bean
    public Queue shenshiRetryQueue() {
        return QueueBuilder.durable("video-analyze-shenshi-retry")
                .deadLetterExchange("ex_video-analyze-shenshi")
                .ttl(RETRY_DELAY)
                .build();
    }

    @Bean
    public FanoutExchange shenshiExchange() {
        return ExchangeBuilder.fanoutExchange("ex_video-analyze-shenshi").build();
    }

    @Bean
    public FanoutExchange shenshiDlxExchange() {
        return ExchangeBuilder.fanoutExchange("ex_video-analyze-shenshi_dlx").build();
    }

    @Bean
    public Binding bindingShenshi(@Qualifier("shenshiQueue")Queue queue, @Qualifier("shenshiExchange")FanoutExchange ex) {
        return BindingBuilder.bind(queue).to(ex);
    }

    @Bean
    public Binding bindingShenshiRetry(@Qualifier("shenshiRetryQueue")Queue queue, @Qualifier("shenshiDlxExchange")FanoutExchange ex) {
        return BindingBuilder.bind(queue).to(ex);
    }
}
